﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;

namespace Skynet.CommonLibrary.Extension
{
    /// <summary>
    /// 将DataSet转换成实体
    /// </summary>
    ///<remarks>
    /// 作者：         李志强
    /// 开发时间：  2011-03-30
    /// </remarks>
    public static class DataSetExtension
    {
        /// <summary>
        /// 将DataSet转换成实体集合
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="ds">需要转换DataSet</param>
        /// <returns>集合</returns>
        public static List<T> ToEntity<T>(this DataSet  ds) where T : class
        {
            if (ds == null )
                return null;
            if (ds.Tables.Count == 0)
                return null;

            return ds.Tables[0].ToEntity<T>();
        }
        /// <summary>
        /// 将DataTable转换成实体集合
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="dt">需要转换DataTable</param>
        /// <returns>集合</returns>
        public static List<T> ToEntity<T>(this DataTable dt) where T : class
        {
            if (dt == null) 
                return null;

            List<T> list = new List<T>();
            foreach (DataRow dr in dt.Rows)
            {
                T entity = typeof(T).CreateInstance() as T;    // Activator.CreateInstance(typeof(T)) as T;
                PropertyInfo[] infos = typeof(T).GetProperties();
                foreach (DataColumn dc in dt.Columns)
                {
                    if (infos.FirstOrDefault(o =>System.String.Compare(o.Name, dc.ColumnName, System.StringComparison.OrdinalIgnoreCase) == 0) != null)
                    {
                        entity.SetValue(dc.ColumnName, dr[dc.ColumnName].ToString());
                    }
                }
                list.Add(entity);
            }
            return list;
        }
        /// <summary>
        /// 将DataTable转换成实体集合，如果转换出现异常，以OUT参数返回
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="dt">需要转换DataTable</param>
        /// <param name="error">所有异常信息</param>
        /// <returns>集合</returns>
        public static List<T> ToEntity<T>(this DataTable dt, out string error) where T : class
        {
            error = string.Empty;
            StringBuilder sb = new StringBuilder();

            if (dt == null)
                return null;

            List<T> list = new List<T>();
            foreach (DataRow dr in dt.Rows)
            {
                T entity = typeof(T).CreateInstance() as T;
                PropertyInfo[] infos = typeof(T).GetProperties();
                foreach (DataColumn dc in dt.Columns)
                {
                    try
                    {
                        if (infos.FirstOrDefault(o => String.Compare(o.Name, dc.ColumnName, StringComparison.OrdinalIgnoreCase) == 0) != null)
                        {
                            entity.SetValue(dc.ColumnName, dr[dc.ColumnName].ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                        sb.Append(ex.Message + Environment.NewLine);
                    }
                }
                list.Add(entity);
            }
            error = sb.ToString();
            return list;
        }
    }
}

